package com.sgb.kjwl.view.ui.fragment.self.pick;

import android.content.Context;
import androidx.databinding.DataBindingUtil;
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.sgb.kjwl.AppFragment;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.FragmentUserMaterialBinding;
import com.sgb.kjwl.di.self.DaggerSelfFragComponent;
import com.sgb.kjwl.di.self.SelfFragModule;
import com.sgb.kjwl.model.entity.user.UserMaterialEntity;
import com.sgb.kjwl.view.ui.activity.self.pick.MaterialPriceListActivity;
import com.sgb.kjwl.view.ui.adapter.self.pick.UserMaterialAdapter;
import com.sgb.kjwl.viewmodel.self.pick.UserMaterialFragModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.xrecyclerview.XRecyclerView;

import java.util.List;

import javax.inject.Inject;

public class UserMaterialFragment extends AppFragment {
    public static final String TAG = "";
    private FragmentUserMaterialBinding binding;
    @Inject
    UserMaterialFragModel viewModel;
    private UserMaterialAdapter userMaterialAdapter;

    private String id = null;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
    }

    @Override
    protected void setupFragmentComponent(AppComponent appComponent) {
        DaggerSelfFragComponent.builder().appComponent(appComponent).selfFragModule(new SelfFragModule(this)).build().inject(this);
    }

    public static UserMaterialFragment newInstance(String id) {
        UserMaterialFragment fragment = new UserMaterialFragment();
        Bundle bundle = new Bundle();
        bundle.putString("id", id);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_user_material, container, false);
        binding.setViewModel(viewModel);
        return binding.getRoot();
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
        Bundle bundle =this.getArguments();//得到从Activity传来的数据
        if(bundle!=null){
            id = bundle.getString("id");
        }
        viewModel.getData(id);
    }

    private void initView() {
        if (userMaterialAdapter == null) {
            userMaterialAdapter = new UserMaterialAdapter(getActivity(), null);
        }

        userMaterialAdapter.setOnItemClick(new AdapterOnItemClick<UserMaterialEntity>() {
            @Override
            public void onItemClick(UserMaterialEntity userMaterialEntity, int position) {
                MaterialPriceListActivity.start(getActivity(), userMaterialEntity);
            }
        });

        binding.recyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 1));
        binding.recyclerView.setAdapter(userMaterialAdapter);

        binding.recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                viewModel.getData(id);
                binding.recyclerView.setLoadingMoreEnabled(true);
            }

            @Override
            public void onLoadMore() {
                if (userMaterialAdapter.getDatas().size() < viewModel.total){
                    viewModel.getMoreData(id);
                }else {
                    binding.recyclerView.loadMoreComplete();
                }
            }
        });
    }

    public void showData(List<UserMaterialEntity> userMaterialEntities){
        userMaterialAdapter.setDatas(userMaterialEntities);
        binding.recyclerView.refreshComplete();

        if (userMaterialEntities.size()>0){
            binding.emptyView.setVisibility(View.GONE);
        }else {
            binding.emptyView.setVisibility(View.VISIBLE);
        }
    }

    public void showMoreData(List<UserMaterialEntity> userMaterialEntities){
        binding.recyclerView.loadMoreComplete();
        userMaterialAdapter.addDatas(userMaterialEntities);
    }

}
